package com.proposition.web.paper;

import com.proposition.commons.constant.ErrorCode;
import com.proposition.commons.constant.PaperPermission;
import com.proposition.commons.model.ExamCountForPaper;
import com.proposition.paper.service.PaperAutoAddService;
import com.proposition.paper.service.PaperService;
import com.proposition.support.utils.Rest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 自动组卷
 *
 * @author 杨凯
 * @create 2017-12-28 14:07
 **/
@Slf4j
@Controller
@RequestMapping("/automatic")
public class PaperAutoController {

    @Autowired
    private PaperService paperService;

    @Autowired
    private PaperAutoAddService paperAutoAddService;

	/**
	 * 自动组卷
	 * @return
	 */
	@ResponseBody
    @PostMapping("/generate")
    public Rest autoGenerate() {
        Rest.Builder rest = Rest.newBuilder();
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isPermitted(PaperPermission.ADD_PAPER)) {
            //没有权限
            return rest.status(ErrorCode.PERMISSION_DENIED).build();
        }
        try {
        	//获取通过审核的试卷数量
            ExamCountForPaper count = paperService.getExamCount(4);
            if (count.getCRSFCount() < 40 || count.getKGHCount() < 40 || count.getCZCount() < 40 || count.getCZZDCount() < 40 || count.getCRXWCount() < 40 || count.getEKSFCount() < 40 || count.getEKTNCount() < 40 || count.getEKXWCount() < 40 || count.getEKZTCount() < 40 || count.getFKAMCount() < 40 || count.getFKZTCount() < 40 || count.getNKZTCount() < 40 || count.getNKAMCount() < 40 || count.getSKZTCount() < 40 || count.getXWCount() < 40) {
                //试题数量不足
                return rest.status(2001).build();
            }
            paperAutoAddService.generateAllPatternPaper();
        } catch (Exception e) {
            log.error("人工组卷出息异常[{}]", e.getMessage());
            //出现异常
            return rest.status(1002).build();
        }
        return rest.status(1001).build();
    }
}
